package main

import (
	"blockchains-aio/conf"
	"blockchains-aio/db"
	"blockchains-aio/gin"
	"blockchains-aio/util"
	"flag"
	"log"
)

var (
	// port                 = 39781
	// BLOCKCHAINS_AIO_HOME = "~/.blockchains_aio"
	IsVersion bool
)

// https://protobuf.dev/reference/go/faq/
// At program execution. The behavior for handling conflicts when executing a particular Go binary can be set with an environment variable:
// GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn ./main
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	// -port=39781
	flag.IntVar(&conf.Options.Port, "port", conf.Options.Port, "the port to which the docker proxy is listening")
	flag.StringVar(&conf.Options.Home, "data", conf.Options.Home, "blockchain-aio data folder")
	flag.BoolVar(&IsVersion, "version", false, "version info")
	flag.Parse()

	db.InitDB()

	//log.Println("data folder:", conf.Options.Home)
	//log.Println("Contract Rep Templates Folder: " + conf.GetContractRepTemplatesFolder())
	//log.Println("Contract Rep Individuals Folder: " + conf.GetContractRepIndividualsFolder())

	if !IsVersion {
		buildPathes()
		if err := gin.Run(conf.Options.Port); err != nil {
			log.Fatalf("Internal server error: %v", err)
		}
	} else {
		log.Println(conf.CompiledDate)
	}

}

func buildPathes() {
	makeFolders(conf.GetContractRepTemplatesFolder(),
		conf.GetContractRepIndividualsFolder(),
		conf.GetStackTemplateFolder(),
		conf.GetStackConfigFolder(),
		conf.GetNfsFolder())
}

func makeFolders(folders ...string) {
	for _, folder := range folders {
		if !util.IsPathExists(folder) {
			util.CreateAllDir(folder)
		}
	}
}

//func makeContractPathes() {
//	folder := conf.GetContractRepTemplatesFolder()
//	if !util.IsPathExists(folder) {
//		util.CreateAllDir(folder)
//	}
//
//	folder = conf.GetContractRepIndividualsFolder()
//	if !util.IsPathExists(folder) {
//		util.CreateAllDir(folder)
//	}
//}
//
//func makeStackPathes() {
//	folder := conf.GetStackTemplateFolder()
//	if !util.IsPathExists(folder) {
//		util.CreateAllDir(folder)
//	}
//
//	folder = conf.GetStackConfigFolder()
//	if !util.IsPathExists(folder) {
//		util.CreateAllDir(folder)
//	}
//}
//
//func makeNfs() {
//	folder := conf.GetNfsFolder()
//	if !util.IsPathExists(folder) {
//		util.CreateAllDir(folder)
//	}
//}
